package com.wlc.collection;

import java.util.*;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description 测试容器类
 * @date 2021-07-13 08:46
 */
public class TestCollection {
    /**
     * 初始化List 集合
     *
     * @param total
     * @return
     */
    public List<Hero> initList(int total) {
        List<Hero> list = new ArrayList<>();
        for (int i = 0; i < total; i++) {
            list.add(new Hero("hero-" + i));
        }
        System.out.println("初始化List完成");
        return list;
    }

    /**
     * 初始化HashMap
     *
     * @param total
     * @return
     */
    public HashMap<String, Hero> initMap(int total) {
        HashMap<String, Hero> list = new HashMap<>();
        for (int i = 0; i < total; i++) {
            list.put("hero-" + i, new Hero("hero-" + i));
        }
        System.out.println("初始化Map完成");
        return list;
    }

    /***
     * 定义200000个对象，从这些对象中查找对象名称叫做 hero-2000000 的对象名称
     * 看几次可以查找到，耗时为多少
     * 分别用ArrayList 和 HashMap 查看
     *
     */
    public void method01() {
        List<Hero> heroes = initList(2000 * 100);
        int total = heroes.size();
        int number = 0;
        Collections.shuffle(heroes);
        for (int j = 0; j < 10; j++) {

            long start = System.currentTimeMillis();
            for (int i = 0; i < total; i++) {
                if ("hero-20000".equals(heroes.get(i).getName())) {
                    break;
                }
                number++;
            }
            long end = System.currentTimeMillis();
            System.out.printf("循环了%d次，每次查找了%d次,耗时%d", j, number, end - start);
            System.out.println("");
        }
    }

    /**
     * 遍历10次，使用HashMap查找  hero-20000的对象
     */
    public void method02() {
        HashMap<String, Hero> map = initMap(2000 * 100);
        int number = 0;
        for (int j = 0; j < 10; j++) {

            long start = System.currentTimeMillis();
            Hero hero = map.get("hero-20000");
            System.out.println("hero.getName() = " + hero.getName());
            long end = System.currentTimeMillis();
            System.out.printf("map 循环了%d次,耗时%d", j, end - start);
            System.out.println("");
        }
    }

    public void method03() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("abc", null);
        System.out.println(map.get("abc"));
        int number,age;
    }

    public static void main(String[] args) {
        TestCollection testCollection = new TestCollection();
        testCollection.method03();
    }
}
